import * as CONSTANT from '../constant';

//01 定义初始state
const defaultState = {name:'unknown',age:18}  // js 任何有效值

//02 定义reducer : 根据action执行reducer 
const reducer  = (state=defaultState,action)=>{
    // console.log(action);
    // console.log('action',action,action.type,action.payload);
    // let newState  = JSON.parse(JSON.stringify(state))
    switch (action.type) {
        case CONSTANT.INPUT_NAME:
            let name = action.payload  //name
            let newState  = JSON.parse(JSON.stringify(state)) //利用JSON.parse(JSON.stringify) copy
            newState.name = name
            return newState
        // case CONSTANT.INPUT_AGE:
        //     let age = action.payload  // age
        //     return {...state,age:age} //=>Object.assign(target,value)  //先state解包 后覆盖
        case CONSTANT.INPUT_AGE:
            let age = action.payload  // age
            state.age  = age //先覆盖
             return {...state} //然后state解包 放在新的对象里
            // return state  // 对比 返回原有的对象（引用）
        case "COMMIT" :
            //模拟提交
            //数据
            // const {name,age} = state

            //网络API
            for (let i = 0; i < 100; i++) {
                console.log(i);
            }

            //提交结果
            return {mag:"commit ok",result:1,data:state}
        default:
           return state
    }
}

export default reducer